#include <Riostream.h>
#include <TVirtualMC.h>
#include <TLorentzVector.h>
#include <TTree.h>
#include <math.h> 
#include <cstdlib>

#include "sPHENIX_GEMSD.h"
#include "sPHENIX_RootManager.h"

/// \cond CLASSIMP
ClassImp(sPHENIX_GEMSD)
/// \endcond
 
using namespace std;
 
//_____________________________________________________________________________
sPHENIX_GEMSD::sPHENIX_GEMSD(const char* name, 
                                     sPHENIX_DetectorConstruction* detector)
  : TNamed(name, ""),
    Detector(detector),
    GEMHitCollection(0),
    GEMVolId(0),
    VerboseLevel(2),    // 1 For PrintTotal, 2 for Print
    counter(0)
{
  /// Standard constructor.
  /// Create hits collection and an empty hit for each layer
  /// As the copy numbers may start from 0 or 1 (depending on
  /// geometry model, we create one more layer for this case.)
  /// \param name      The calorimeter hits collection name
  /// \param detector  The detector construction
}
 
//_____________________________________________________________________________
sPHENIX_GEMSD::sPHENIX_GEMSD()
  : TNamed(),
    Detector(0),
    GEMHitCollection(0),
    GEMVolId(0),
    VerboseLevel(0),
    counter(0)
{
  /// Default constructor
}
 
//_____________________________________________________________________________
sPHENIX_GEMSD::~sPHENIX_GEMSD()
{
  /// Destructor
 
  //  if (GEMHitCollection) GEMHitCollection.Delete();
  //  delete GEMHitCollection;
}
 
//
// private methods
//
 
//_____________________________________________________________________________
sPHENIX_GEMHit* sPHENIX_GEMSD::GetHit(Int_t i) const
{
  /// \return   The hit i
  /// \param i  The hit i

  return GEMHitCollection[i];
}

//_____________________________________________________________________________
void  sPHENIX_GEMSD::ResetHits()
{
  /// Reset all hits in the hits collection.

  GEMHitCollection.clear();
} 
 
//
// public methods
//
 
//_____________________________________________________________________________
void sPHENIX_GEMSD::Initialize()
{
  /// Register hits collection in the Root manager;
  /// set sensitive volumes.
   
  Register();

  GEMVolId = gMC->VolId("fsgemvol");
  
  if(GEMVolId == 0)
    GEMVolId = gMC->VolId("Carbon");
  
}
 
//_____________________________________________________________________________
Bool_t sPHENIX_GEMSD::ProcessHits()
{
  /// Account energy deposit and track lengths for each layer in its hit.
  
  TLorentzVector* pos = new TLorentzVector();
  Int_t GEMCopyNo;
  Bool_t flag = false;
  Int_t pid = gMC->TrackPid();
  Int_t charge = gMC->ParticleCharge(pid);

  // cout << " -----------------charge = " << charge << "     name = " << gMC->ParticleName(pid) << endl;

  if(gMC->IsTrackEntering() && (charge != 0)){
    Int_t id = gMC->CurrentVolID(GEMCopyNo);

    if(id != GEMVolId)
      return false;
    
    sPHENIX_GEMHit* newHit = new sPHENIX_GEMHit();

    gMC->TrackPosition(*pos);

    newHit->SetGEMHitLoc(pos);
    newHit->SetGEMCopyNo(GEMCopyNo);
    GEMHitCollection.push_back(newHit);
    
    return true;
  }
  else
    return false;
}
 
//_____________________________________________________________________________
void sPHENIX_GEMSD::EndOfEvent()
{
  /// Print hits collection (if verbose) and reset hits afterwards.
 
  if (VerboseLevel>1)  Print();
  //else if(VerboseLevel>0)  PrintTotal();


  // Reset hits collection
  ResetHits();  
}
 
//_____________________________________________________________________________
void sPHENIX_GEMSD::Register()
{
  /// Register the hits collection in Root manager.
   
  GEMHitCollectionPtr = &GEMHitCollection;
  sPHENIX_RootManager::Instance()
    ->Register("gemhits", "vector<sPHENIX_GEMHit*>", &GEMHitCollectionPtr);
}
 
//_____________________________________________________________________________
void sPHENIX_GEMSD::Print(Option_t* /*option*/) const
{
  /// Print the hits collection.
  
  for (Int_t i=0; i<GEMHitCollection.size(); i++) GEMHitCollection[i]->Print();     
}
 
